using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using zhwy.saas.api.Models.Other.SelectControl;

namespace zhwy.saas.api.Extensions.Enum
{
    /// <summary>
    /// 枚举转换工具类，用于将枚举类型转换为List<QueryEnumRes>
    /// </summary>
    public static class EnumToListConverter
    {
        /// <summary>
        /// 将枚举类型转换为List<QueryEnumRes>
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <returns>转换后的List<QueryEnumRes>集合</returns>
        public static List<QueryEnumRes> ToQueryEnumList<TEnum>() where TEnum : struct, System.Enum
        {
            var result = new List<QueryEnumRes>();
            
            var enumType = typeof(TEnum);
            if (!enumType.IsEnum)
            {
                return result;
            }
            
            foreach (var value in System.Enum.GetValues(enumType))
            {
                var field = enumType.GetField(value.ToString());
                var description = field?.GetCustomAttribute<DescriptionAttribute>()?.Description ?? value.ToString();
                
                result.Add(new QueryEnumRes
                {
                    Id = Convert.ToInt32(value),
                    Name = value.ToString(),
                    Description = description
                });
            }
            
            return result;
        }
        
        /// <summary>
        /// 将指定枚举类型转换为List<QueryEnumRes>（根据枚举类型的完整名称）
        /// </summary>
        /// <param name="enumTypeName">枚举类型的完整名称，例如："zhwy.saas.api.Extensions.Enum.EnumHelper+UsersStatus"</param>
        /// <returns>转换后的List<QueryEnumRes>集合</returns>
        public static List<QueryEnumRes> ToQueryEnumList(string enumTypeName)
        {
            var result = new List<QueryEnumRes>();
            
            var enumType = Type.GetType(enumTypeName);
            if (enumType == null || !enumType.IsEnum)
            {
                return result;
            }
            
            foreach (var value in System.Enum.GetValues(enumType))
            {
                var field = enumType.GetField(value.ToString());
                var description = field?.GetCustomAttribute<DescriptionAttribute>()?.Description ?? value.ToString();
                
                result.Add(new QueryEnumRes
                {
                    Id = Convert.ToInt32(value),
                    Name = value.ToString(),
                    Description = description
                });
            }
            
            return result;
        }
        
        /// <summary>
        /// 获取指定枚举值的描述
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="value">枚举值</param>
        /// <returns>枚举值的描述</returns>
        public static string GetEnumDescription<TEnum>(TEnum value) where TEnum : struct, System.Enum
        {
            var field = typeof(TEnum).GetField(value.ToString());
            if (field == null) return value.ToString();
            
            var attribute = field.GetCustomAttribute<DescriptionAttribute>();
            return attribute?.Description ?? value.ToString();
        }
    }
}
